-- 薇洛辅助

local _G = GLOBAL
local e_util = require "libs/entutil"
local p_util = require "libs/playerutil"
local key1,key2 = GetModConfigData("willow_bind1"),GetModConfigData("willow_bind2")


local function InputFn(key, fn)
    if key then
        _G.TheInput:AddKeyDownHandler(key, function()
            if e_util:InGame() == "willow" then
                fn()
            end
        end)
    end
end

-- 余烬使用
InputFn(key1, function()
    local ember = p_util:GetItemFromAll("willow_ember", nil, function(e) return e.components and e.components.spellbook end, "hands")
    local hud = _G.ThePlayer.HUD
    if hud:IsSpellWheelOpen() then
        hud:CloseSpellWheel()
    elseif ember then
        ember.components.spellbook:OpenSpellBook(_G.ThePlayer)
    end
end)
local memo_value
local memory = {}
InputFn(key2, function()
    ------适配---------
    local c_mana = require("libs/ctrlmanager")
    -------------------
    -- 检查玩家是否装备打火机
    local lighter = p_util:IsEquipped("lighter")
    if lighter then
        if p_util:CanTakeItem(lighter) then
            ----- 适配 ------
            memory.value = memo_value
            c_mana:SetBtnSlotData("walk", "hands", memory)
            ----------------
            p_util:UnEquip(lighter)
        end
    else
        lighter = p_util:GetItemFromAll("lighter", nil, function(e) return e_util:GetItemEquipSlot(e) end, "hands")
        if lighter then
            ---- 适配 -----
            memory = c_mana:GetBtnSlotData("walk", "hands")
            memo_value = memory.value
            memory.value = false
            c_mana:SetBtnSlotData("walk", "hands", memory)
            --------------
            p_util:Equip(lighter)
            e_util:WaitToDo(_G.ThePlayer, 0.1, 5, function()
                return p_util:GetInvAction(lighter, true, "START_CHANNELCAST")
            end, function(act)
                p_util:DoAction(act, _G.RPC.UseItemFromInvTile, act.action.code, lighter)
            end)
        end
    end
end)